#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
float Dist2Points(float x1,float y1,float x2,float y2)
{
    float result;
    result=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    return result;
}
int isTwpCirclesIntersect(float x1,float y1,float r1,float x2,float y2,float r2)
{
    if(Dist2Points(x1,y1,x2,y2)<r1+r2)
    return 1;
    return 0;
}
void DrawCircles1(float x,float y,float r)
{
    setlinecolor(RGB(0,0,0));
    setfillcolor(RGB(255,255,0));
    fillcircle(x,y,r);
}
void DrawCircles2(float x,float y,float r)
{
    float h=rand()&360;
    COLORREF color=HSVtoRGB(h,0.6,0.8);
    setlinecolor(RGB(255,255,255));
    setfillcolor(color);
    fillcircle(x,y,r);
}
void DrawCircles3(float x,float y,float r)
{
    while(r>0)
    {
        float h=rand()%360;
        COLORREF color=HSVtoRGB(h,0.6,0.8);
        setlinecolor(RGB(255,255,255));
        setfillcolor(color);
        fillcircle(x,y,r);
        r=r-5;
    }
}
void DrawCircles4(float x,float y,float r)
{
    while(r>0)
    {
        float h=rand()%360;
        COLORREF color=HSVtoRGB(h,0.9,0.8);
        setlinecolor(color);
        fillcircle(x,y,r);
        r=r-5;    
    }
}
int main()
{
	int width = 600;
	int height = 600;
	initgraph(width, height);
	setbkcolor(RGB(255, 255, 255));
	cleardevice();
	srand(time(0));
	int xArray[1000];
	int yArray[1000];
	int rArray[1000];
	int rmin = 8;
	int rmax = 50;
	int circleNum = 0;
	float x,y,r;
    int isNewCircleOK;
    int i,j;
    int drawMode=3;
	while(circleNum<1000)
	{
        isNewCircleOK=0;
        while(isNewCircleOK==0)
        { 
            if(_kbhit())
            {
                char input=_getch();
                if(input==' ')
                {
                    circleNum=0;
                    cleardevice();
                    drawMode=drawMode+1;
                    if(drawMode>4)
                    drawMode=1;
                }
            }
        x= rand() % width;
		y= rand() % height;
		r= rmin;
        for(i=0;i<circleNum;i++)
            if(isTwpCirclesIntersect(xArray[i],yArray[i],rArray[i],x,y,r))
                break;
                if(i==circleNum)
                isNewCircleOK=1;
        }
        isNewCircleOK=0;
        while(isNewCircleOK==0&&r<rmax)
        {
            r++;
            for(j=0;j<circleNum;j++)
            {
                if(isTwpCirclesIntersect(xArray[j],yArray[j],rArray[j],x,y,r))
                {
                    isNewCircleOK=1;
                    break;
                }
            }
        }
        xArray[circleNum]=x;
        yArray[circleNum]=y;
        rArray[circleNum]=r;
        circleNum++;
        if(drawMode==1)
        DrawCircles1(x,y,r);
        if(drawMode==2)
        DrawCircles2(x,y,r);
        if(drawMode==3)
        DrawCircles3(x,y,r);
        if(drawMode==4)
        DrawCircles4(x,y,r);
        Sleep(10);
	}
	_getch();
	closegraph();
	return 0;

}